package com.we823.project.repository.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

public class AbstractBaseService<T extends Serializable> implements BaseService<T, Integer> {

	private JpaRepository<T, Integer> baseJpaRepository;
	
	public AbstractBaseService(JpaRepository<T, Integer> baseJpaRepository){
		this.baseJpaRepository = baseJpaRepository;
	}
	@Override
	public T save(T entity) {
		return baseJpaRepository.save(entity);
	}

	@Override
	public T update(T entity) {
		return baseJpaRepository.saveAndFlush(entity);
	}

	@Override
	public T findOne(Integer key) {
		return baseJpaRepository.findOne(key);
	}

	@Override
	public List<T> findAll() {
		return baseJpaRepository.findAll();
	}

	@Override
	public List<T> findAll(Sort sort) {
		return baseJpaRepository.findAll(sort);
	}

	@Override
	public Page<T> findAll(Pageable pageable) {
		if (pageable!=null) {
			return baseJpaRepository.findAll(pageable);
		}else {
			return new PageImpl<T>(findAll());
		}
		
	}
	@Override
	public void delete(Integer key) {
		baseJpaRepository.delete(key);		
	}

}
